#include "manifest.h"
#ifdef ANDROID
#include <android/log.h>
#endif

#ifndef NDEBUG

#define PREFIX "jni/"

bool assert0(const char *condition, const char* file, int line, const char* func, const char *fmt, ...) {
    va_list vl;
    va_start(vl, fmt);
    char buf[16*1024];
    if (file != null) {
        char* prefix = strstr(file, PREFIX);
        if (prefix != null) {
            file = prefix + strlen(PREFIX);
        }
        if (func != null) {
           sprintf(buf, "%s:%d %s ", file, line, func);
        } else {
           sprintf(buf, "%s:%d ", file, line);
        }
        sprintf(buf + strlen(buf), "assertion \"%s\" failed ", condition);
        vsprintf(buf + strlen(buf), fmt, vl);
    } else {
        sprintf(buf, "assertion \"%s\" failed ", condition);
        vsprintf(buf + strlen(buf), fmt, vl);
    }
    va_end(vl);
    __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "%s", buf);
    exit(1);
    return false;
}

static void log0(const char* file, int line, const char* func, const char *fmt, va_list vl) {
    char buf[16*1024];
    if (file != null) {
        char* prefix = strstr(file, PREFIX);
        if (prefix != null) {
            file = prefix + strlen(PREFIX);
        }
        if (func != null) {
           sprintf(buf, "%s:%d %s ", file, line, func);
        } else {
           sprintf(buf, "%s:%d ", file, line);
        }
        vsprintf(buf + strlen(buf), fmt, vl);
    } else {
        vsprintf(buf, fmt, vl);
    }
#ifdef ANDROID
    __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "%s", buf);
#else
    printf("%s\n", buf);
#endif
}

void trace0(const char* file, int line, const char* func, const char *fmt, ...) {
    va_list vl;
    va_start(vl, fmt);
    log0(file, line, func, fmt, vl);
    va_end(vl);
}

#endif

